package cn.activiti.demo.dimission.controller;

import cn.activiti.demo.dimission.model.Note;
import cn.activiti.demo.dimission.model.ProcessElement;
import cn.activiti.demo.dimission.service.ExpenseAccountActivitiService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class ExpenseAccountActivitiController {

    private ExpenseAccountActivitiService expenseAccountActivitiService;

    @Autowired
    public ExpenseAccountActivitiController(ExpenseAccountActivitiService expenseAccountActivitiService) {
        this.expenseAccountActivitiService = expenseAccountActivitiService;
    }

    @ResponseBody
    @GetMapping("image")
    public JSONArray image(HttpServletResponse response) {
        try {
            Note note = new Note();

            note.setAmount(1000.0);
            note.setInvoice("FP1234234556");
            note.setName("李四");
            note.setSubmitTime(new Date());

            ProcessInstance processInstance = expenseAccountActivitiService.editAndSave(note);

            expenseAccountActivitiService.submit(processInstance.getBusinessKey());

            expenseAccountActivitiService.auditApprove(processInstance.getBusinessKey());

            expenseAccountActivitiService.watchProcess( processInstance.getProcessInstanceId());

            expenseAccountActivitiService.settlement(processInstance.getBusinessKey());

            JSONArray jsonArray = expenseAccountActivitiService.watchProcess( processInstance.getProcessInstanceId());


            expenseAccountActivitiService.watchProcessImage(response.getOutputStream());
            response.getOutputStream().close();

            return jsonArray;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @ResponseBody
    @GetMapping("image2")
    public JSONArray image2(HttpServletResponse response) {
        try {
            Note note = new Note();

            note.setAmount(1000.0);
            note.setInvoice("FP1234234556");
            note.setName("李四");
            note.setSubmitTime(new Date());

            ProcessInstance processInstance = expenseAccountActivitiService.editAndSave(note);

            expenseAccountActivitiService.submit(processInstance.getBusinessKey());

            expenseAccountActivitiService.auditApprove(processInstance.getBusinessKey());

            expenseAccountActivitiService.watchProcess( processInstance.getProcessInstanceId());

            expenseAccountActivitiService.settlement(processInstance.getBusinessKey());

            JSONArray jsonArray = expenseAccountActivitiService.watchProcess( processInstance.getProcessInstanceId());

            expenseAccountActivitiService.watchProcessActivitiedImage(response.getOutputStream(), processInstance.getProcessInstanceId());
            response.getOutputStream().close();

            return jsonArray;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 查看流程图
     * mock api
     * @param response
     * @param processInstanceId
     */
    @ResponseBody
    @GetMapping("watchImage")
    public void watchImage(HttpServletResponse response, String processInstanceId) {
        try {
            expenseAccountActivitiService.watchProcessActivitiedImage(response.getOutputStream(), processInstanceId);
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查看流程历史和下一节点
     * mock api
     * @param response
     * @return
     */
    @ResponseBody
    @GetMapping("watchHistory")
    public List<ProcessElement> watchHistory(HttpServletResponse response, String processInstanceId) {
//        JSONArray jsonArray = expenseAccountActivitiService.watchProcess(processInstanceId);
//
//        System.out.println("下一节点："+expenseAccountActivitiService.getNextTask(processInstanceId));
        List<ProcessElement> processElementList = expenseAccountActivitiService.watchProcessWithNextStep(processInstanceId);

        return processElementList;
    }

    /**
     * 开始一个流程
     * @return
     */
    @ResponseBody
    @GetMapping("startOneProcess")
    public JSONObject startOneProcess() {
        Note note = new Note();

        note.setAmount(1000.0);
        note.setInvoice("FP1234234556");
        note.setName("李四");
        note.setSubmitTime(new Date());
        ProcessInstance processInstance = expenseAccountActivitiService.editAndSave(note);

        JSONObject result = new JSONObject();
        result.put("ProcessInstanceId", processInstance.getProcessInstanceId());
        result.put("id", processInstance.getProcessInstanceId());
        result.put("businessKey", processInstance.getBusinessKey());

        return result;
    }

    /**
     * 提交报销
     * @return
     */
    @ResponseBody
    @GetMapping("submit")
    public JSONObject submit( String processInstanceId, String businessKey) {

        expenseAccountActivitiService.submit(businessKey);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "ok");

        return jsonObject;
    }

    /**
     * 报销批准
     * @param approve
     * @param businessKey
     * @return
     */
    @ResponseBody
    @GetMapping("audit")
    public JSONObject audit( Boolean approve, String businessKey) {

        if (approve) {
            expenseAccountActivitiService.auditApprove(businessKey);
        } else{
            expenseAccountActivitiService.auditReject(businessKey, "报销驳回");
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "ok");

        return jsonObject;
    }
    /**
     * 会计打钱
     * @param approve
     * @param businessKey
     * @return
     */
    @ResponseBody
    @GetMapping("settlement")
    public JSONObject settlement( Boolean approve, String businessKey) {
        expenseAccountActivitiService.settlement(businessKey);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "ok");

        return jsonObject;
    }
    /**
     * reset to beginEvent
     * @param processInstanceId
     * @param businessKey
     * @return
     */
    @ResponseBody
    @GetMapping("reset")
    public JSONObject reset( String processInstanceId, String businessKey) {

        expenseAccountActivitiService.submit(businessKey);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "ok");

        return jsonObject;
    }

    @ResponseBody
    @GetMapping("getNext")
    public JSONObject getNext( String processInstanceId, String businessKey) {

        expenseAccountActivitiService.submit(businessKey);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "ok");

        return jsonObject;
    }


}
